package com.ymlyj666.onlinechat.filter;

import com.alibaba.fastjson.JSON;
import com.ymlyj666.onlinechat.model.Response;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = {"/friends/*", "/message.do"})
public class Filter3_Authentication extends HttpFilter {
    public void destroy() {
    }

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpSession session = req.getSession();
        if (session == null || session.getAttribute("user") == null) {
            res.getWriter().print(JSON.toJSONString(new Response<>(1003, "登录状态已失效，请重新登录！")));
        } else {
            chain.doFilter(req, res);
        }
    }
}
